'use strict';
/**
 * Created by FreezeSoul on 2015/12/9.
 */
define([
        'angular',
        "jquery",
        'app/modules/player/define',
        'app/modules/player/services/player',
        'app/modules/player/directives/add-chart'
    ],
    function(angular,$) {
        angular.module('ad.player')
            .controller('adIndexSelectController',['$scope','$rootScope','$uibModalInstance','_','adPlayerService','layout'
                ,function($scope,$rootScope,$uibModalInstance,_,adPlayerService,layout) {
                    $scope.layout = layout;
                    $scope.items = [];

                    $scope.toggleFolderMenu = function (folder) {
                        folder.hideFolder = !folder.hideFolder;
                    };
                    
                    $scope.loadUserCharts = function () {
                        adPlayerService.loadUserCharts().then(function (data) {
                            var obj = angular.fromJson(data);
                            if(obj.result == 'success'){
                                obj.data.folders.push({
                                    folder_id:adConfig.rootFolderId,
                                    folder_name:'指标根目录'
                                });
                                var folderIds = _.sortBy(_.union(_.map(obj.data.charts, function (chart) {
                                    return chart.chart_folder_id;
                                })));
                                _.each(folderIds, function (folderId) {
                                    var folder = _.findWhere(obj.data.folders,{folder_id:folderId});
                                    if(folder){
                                        $scope.items.push({
                                            folder_id:folder.folder_id,
                                            folder_name:folder.folder_name,
                                            folder_charts: _.sortBy(_.uniq(_.map(_.where(obj.data.charts,{chart_folder_id:folder.folder_id}), function (chart) {
                                                return {
                                                    chart_id:chart.chart_id,
                                                    chart_name:chart.chart_name
                                                }
                                            }), function (chart) {
                                                return chart.chart_id;
                                            }), function (chart) {
                                                return chart.chart_name;
                                            })
                                        })
                                    }
                                });
                            }
                        });
                    };

                    var unbind = $scope.$on('ad.player.sidebar.hide', function(event, arg){
                        $uibModalInstance.close(arg);
                    });
                    $scope.$on('$destroy', unbind);

                    $scope.loadUserCharts();



                }]);
    });